{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "137058c0-8cf9-43e7-b926-54a46d783674",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据预览：\n",
      "        浏览时长      消费频次     收藏商品数    优惠券使用率      历史购买金额  是否高购买概率\n",
      "0  34.967142  2.723471  5.238443  1.061515  453.169325        1\n",
      "1  27.658630  6.158426  5.837174  0.065263  608.512009        1\n",
      "2  25.365823  2.068540  3.209811 -0.656640  155.016433        1\n",
      "3  24.377125  0.974338  3.571237 -0.154012  217.539260        1\n",
      "4  44.656488  2.548447  2.337641 -0.412374  391.123455        1\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "# 生成模拟数据（1000条记录，5个特征）\n",
    "np.random.seed(42)\n",
    "n_samples = 1000\n",
    "# 特征：浏览时长(分钟)、消费频次(月)、收藏商品数、优惠券使用率、历史购买金额(元)\n",
    "features = np.random.randn(n_samples, 5) * np.array([10, 2, 5, 0.5, 200]) + np.array([30, 3, 2, 0.3, 500])\n",
    "# 标签：1=高概率购买，0=低概率购买\n",
    "labels = np.random.binomial(1, 0.6, n_samples)  # 二分类标签\n",
    "\n",
    "# 转换为DataFrame\n",
    "data = pd.DataFrame(features, columns=['浏览时长', '消费频次', '收藏商品数', '优惠券使用率', '历史购买金额'])\n",
    "data['是否高购买概率'] = labels\n",
    "print(\"数据预览：\")\n",
    "print(data.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fbcca0a7-72c6-47b8-a0eb-f7ae3ad8626a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "模型评估结果：\n",
      "      模型       准确率       精确率       召回率      F1分数\n",
      "0   逻辑回归  0.610000  0.620690  0.962567  0.754717\n",
      "1    决策树  0.553333  0.648045  0.620321  0.633880\n",
      "2   随机森林  0.586667  0.631799  0.807487  0.708920\n",
      "3  梯度提升树  0.573333  0.621399  0.807487  0.702326\n",
      "4  支持向量机  0.623333  0.623333  1.000000  0.767967\n"
     ]
    }
   ],
   "source": [
    "# 拆分训练集与测试集（7:3）\n",
    "X = data.drop('是否高购买概率', axis=1)\n",
    "y = data['是否高购买概率']\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n",
    "\n",
    "# 定义5个模型\n",
    "models = {\n",
    "    '逻辑回归': LogisticRegression(),\n",
    "    '决策树': DecisionTreeClassifier(random_state=42),\n",
    "    '随机森林': RandomForestClassifier(random_state=42),\n",
    "    '梯度提升树': GradientBoostingClassifier(random_state=42),\n",
    "    '支持向量机': SVC(probability=True, random_state=42)\n",
    "}\n",
    "\n",
    "# 训练并评估每个模型\n",
    "results = []\n",
    "for name, model in models.items():\n",
    "    model.fit(X_train, y_train)\n",
    "    y_pred = model.predict(X_test)\n",
    "    # 计算评估指标\n",
    "    acc = accuracy_score(y_test, y_pred)\n",
    "    prec = precision_score(y_test, y_pred)\n",
    "    rec = recall_score(y_test, y_pred)\n",
    "    f1 = f1_score(y_test, y_pred)\n",
    "    results.append({\n",
    "        '模型': name,\n",
    "        '准确率': acc,\n",
    "        '精确率': prec,\n",
    "        '召回率': rec,\n",
    "        'F1分数': f1\n",
    "    })\n",
    "\n",
    "# 转换为DataFrame便于分析\n",
    "results_df = pd.DataFrame(results)\n",
    "print(\"\\n模型评估结果：\")\n",
    "print(results_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfbacbe1-7d63-4d1e-96d7-12ecd559f139",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
